package br.com.northwind.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.com.northwind.database.Conexao;
import br.com.northwind.exceptions.ConexaoException;
import br.com.northwind.exceptions.DriverException;
import br.com.northwind.fabricas.FabricaConexao;
import br.com.northwind.interfaces.IOrderDetailsDao;
import br.com.northwind.tabelas.OrderDetails;

public class OrderDetailsDao implements IOrderDetailsDao {

	private final String SQL_INSERT = "INSERT INTO OrderDetails ( ProductId, UnitPrice, Quantity, Discount ) VALUES (?,?,?,?)";
	private final String SQL_UPDATE = "UPDATE OrderDetails SET ProductId = ?, UnitPrice = ?, Quantity = ?, Discount = ? WHERE OrderId = ?";
	private final String SQL_DELETE = "DELETE FROM OrderDetails WHERE OrderId = ?";
	private final String SQL_SELECT_TODOS = "SELECT * FROM OrderDetails";
	private final String SQL_SELECT_BY_CODIGO = "SELECT * FROM OrderDetails WHERE OrderId = ?";

	private Conexao conexao;
	private PreparedStatement statement = null;
	private ResultSet rs = null;

	public OrderDetailsDao() {
		this.conexao = FabricaConexao.criarConexao();
	}

	public OrderDetailsDao(Conexao conexao) {
		this.conexao = conexao;
	}

	public void inserir(OrderDetails _orderdetails) throws ConexaoException,DriverException {
		try {
			this.conexao.abrirConexao();
			this.statement = this.conexao.getConexao().prepareStatement(this.SQL_INSERT);
			
			this.statement.setInt(1, _orderdetails.getProductId());
			this.statement.setDouble(2, _orderdetails.getUnitPrice());
			this.statement.setInt(3, _orderdetails.getQuantity());
			this.statement.setInt(4, _orderdetails.getDiscount());
			
			this.statement.execute();
		} catch (SQLException e) {
			throw new ConexaoException();
		} catch (ClassNotFoundException e) {
			throw new DriverException();
		} finally {
			try {
				
				if (this.conexao != null) this.conexao.fecharConexao();
				if (this.statement != null) this.statement.close();
				
			} catch (SQLException e) {
				throw new ConexaoException();
			}
		}
	}

	public void editar(OrderDetails _orderdetails) throws ConexaoException,DriverException {
		PreparedStatement update = null;
		try {
			this.conexao.abrirConexao();
			this.statement = this.conexao.getConexao().prepareStatement(this.SQL_SELECT_BY_CODIGO);
			
			this.statement.setInt(1, _orderdetails.getOrderId());
			
			this.rs = statement.executeQuery();

			if (rs.next()) {
				update = this.conexao.getConexao().prepareStatement(this.SQL_UPDATE);
				
				
				update.setInt(1, _orderdetails.getProductId());
				update.setDouble(2, _orderdetails.getUnitPrice());
				update.setInt(3, _orderdetails.getQuantity());
				update.setInt(4, _orderdetails.getDiscount());

				update.execute();
				update.close();
			}
		} catch (SQLException e) {
			throw new ConexaoException();
		} catch (ClassNotFoundException e) {
			throw new DriverException();
		} finally {
			try {
				if (this.conexao != null) this.conexao.fecharConexao();
				if (this.statement != null) this.statement.close();
				if (this.rs != null) this.rs.close();
				if (update != null) update.close();
			} catch (SQLException e) {
				throw new ConexaoException();
			}
		}

	}

	public void excluir(Integer _orderid) throws ConexaoException,DriverException {
		try {
			this.conexao.abrirConexao();
			this.statement = this.conexao.getConexao().prepareStatement(this.SQL_DELETE);
			
			this.statement.setInt(1, _orderid);
			
			this.statement.execute();

		} catch (SQLException e) {
			throw new ConexaoException();
		} catch (ClassNotFoundException e) {
			throw new DriverException();
		} finally {
			try {
				
				if (this.conexao != null) this.conexao.fecharConexao();
				if (this.statement != null) this.statement.close();
				if (this.rs != null) this.rs.close();
				
			} catch (SQLException e) {
				throw new ConexaoException();
			}
		}
	}

	public List<OrderDetails> selecionarAll() throws ConexaoException,DriverException {
		List<OrderDetails> listaDeOrderDetails = new ArrayList<OrderDetails>();
		try {
			this.conexao.abrirConexao();
			this.statement = this.conexao.getConexao().prepareStatement(this.SQL_SELECT_TODOS);
			this.rs = this.statement.executeQuery();

			while (rs.next()) {
				
				int OrderId = this.rs.getInt(1);
				int ProductId = this.rs.getInt(2);
				double UnitPrice = this.rs.getDouble(3);
				int Quantity = this.rs.getInt(4);
				int Discount = this.rs.getInt(5);
				
				listaDeOrderDetails.add(new OrderDetails(OrderId, ProductId, UnitPrice, Quantity, Discount));
				
			}

		} catch (SQLException e) {
			throw new ConexaoException();
		} catch (ClassNotFoundException e) {
			throw new DriverException();
		} finally {
			try {
				if (this.conexao != null) this.conexao.fecharConexao();
				if (this.statement != null) this.statement.close();
			} catch (SQLException e) {
				throw new ConexaoException();
			}
		}
		return listaDeOrderDetails;
	}

	public OrderDetails selecionarByCod(Integer _orderid) throws ConexaoException, DriverException {
		OrderDetails localizado = null;
		try {
			this.conexao.abrirConexao();
			this.statement = this.conexao.getConexao().prepareStatement(this.SQL_SELECT_BY_CODIGO);
			this.statement.setInt(1, _orderid);
			this.rs = this.statement.executeQuery();

			if (rs.next()) {
				
				int OrderId = this.rs.getInt(1);
				int ProductId = this.rs.getInt(2);
				double UnitPrice = this.rs.getDouble(3);
				int Quantity = this.rs.getInt(4);
				int Discount = this.rs.getInt(5);
				
				localizado = new OrderDetails(OrderId, ProductId, UnitPrice, Quantity, Discount);
				
			}

		} catch (SQLException e) {
			throw new ConexaoException();
		} catch (ClassNotFoundException e) {
			throw new DriverException();
		} finally {
			try {
				if (this.conexao != null) this.conexao.fecharConexao();
				if (this.statement != null) this.statement.close();
			} catch (SQLException e) {
				throw new ConexaoException();
			}
		}
		return localizado;
	}

}
